package serverside;

import java.sql.SQLException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class BillPayment extends Transaction {

    private Keypad keypad;
    private Scanner input;
    String tmp;
    public BillPayment(int _cardID, BankDatabase atmBankDatabase, Screen atmScreen, Keypad atmKeypad) {
        super(_cardID, atmBankDatabase, atmScreen);
        keypad = atmKeypad;
        input = new Scanner(System.in);
    }

    @Override
    public void execute() {
        try {
            System.out.println("Transaction::BillPayment");
            BankDatabase database = getBankDatabase();
            Screen screen = getScreen();

            screen.sendDataToClient("You can pay.");
            String[] list = ATM.separate(tmp = keypad.getInput());
            System.out.println("Transaction::BillPayment client sent: " + 
                tmp);
            String result = database.bill_payment(getCardID(), list[0], Integer.parseInt(list[1]),
                    Integer.parseInt(list[2]), Integer.parseInt(list[3]));
            screen.sendDataToClient(result);
             System.out.println("Transaction::BillPayment result: " + result +
                    "\n");
        } catch (SQLException ex) {
            System.out.println("An error occured in performing BillPayment releated to database");
            System.out.print("If you want to view error send 1 or 0 otherwise");
            if (input.nextInt() == 1) {
                JOptionPane.showMessageDialog(null, "Error", ex.getMessage(), JOptionPane.ERROR_MESSAGE);
            }
        }
    }

}
